package leetcode_回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * 子集
 * 数组中的元素都是唯一的
 * 就是组合，只是组合的个数不同
 */
public class _78 {
    public static void main(String[] args) {
        System.out.println(new _78().subsets(new int[]{1, 2, 3}));
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> tem = new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        ans.add(new ArrayList<>(tem));
        for (int i = 1; i <= nums.length; i++) {
            dfs(nums, 0, i, tem, ans);
        }
        return ans;
    }
    public void dfs(int[] nums, int begin, int k, List<Integer> tem, List<List<Integer>> ans) {
        if (tem.size() == k) {
            ans.add(new ArrayList<>(tem));
            return;
        }
        for (int i = begin; i < nums.length - (k - tem.size()) + 1; i++) {
            tem.add(nums[i]);
            dfs(nums, i + 1, k, tem, ans);
            tem.remove(tem.size() - 1);
        }
    }
}
